#!/bin/env python
# -*- coding:cp936 *-*
from visa import *
import wx

'''
Author: Shawn Feng
Email:  FML927@gmail.com
Discription: 依赖 NI-visa驱动, python2.7, PyVisa, wxpython安装包。
             实现与仪器通讯、控制命令的发送和信息读取。类似于NI-max的仪器连接通讯控制工具
Version：0.1-python2.7-cn
'''
class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Cmd Test 1.0 by FML use python", size=(600, 420))
        self.panel = wx.Panel(self, -1)
        
                
        #定义按键并注册事件处理函数
        RefButt = wx.Button(self.panel, label = "刷新仪器", pos=(450, 20), size=(100,30) )        
        self.Bind( wx.EVT_BUTTON, self.RefreshButt, RefButt )

        #定义按键并注册事件处理函数
        TestButt = wx.Button(self.panel, label = "仪器连接测试", pos=(450, 60), size=(100,30) )        
        self.Bind( wx.EVT_BUTTON, self.TestButtClick, TestButt )

        #查找仪器列表
        self.RefreshButt(wx.EVT_BUTTON)
        
        #send 
        wx.StaticText(self.panel, -1, "发送命令：", (20,70), (100,20))
        self.sendBox = wx.TextCtrl(self.panel, -1, "*idn?", pos = (20,90), size=(400,100))
                        
        #read text
        wx.StaticText(self.panel, -1, "返回结果：", (20,200), (100,20))
        self.readBox = wx.TextCtrl(self.panel, -1, "", pos = (20,220), size=(400,100))
        
        #三个按钮
        SendButt = wx.Button(self.panel, label = "SEND", pos=(20, 340), size=(100,30) )        
        self.Bind( wx.EVT_BUTTON, self.SendButtClick, SendButt )  
        
        ReadButt = wx.Button(self.panel, label = "READ", pos=(220, 340), size=(100,30) )        
        self.Bind( wx.EVT_BUTTON, self.ReadButtClick, ReadButt )  
        
        SendReadButt = wx.Button(self.panel, label = "SEND && READ", pos=(420, 340), size=(150,30) )        
        self.Bind( wx.EVT_BUTTON, self.SendReadButtClick, SendReadButt )
        
        #self.TestButtClick(wx.EVT_BUTTON)
               
    def SendButtClick(self, event):
        if self.Conn:
            self.inst.write(self.sendBox.GetValue())
        
    def ReadButtClick(self, event):
        if self.Conn:
            self.readBox.Clear()   
            self.readBox.AppendText( self.inst.read())
        
    def SendReadButtClick(self, event):
        if self.Conn:
            self.readBox.Clear()
            self.readBox.AppendText( self.inst.ask(self.sendBox.GetValue()) )    
    
        #查找全部仪器 并列出        
    def RefreshButt(self, event): 
        wx.StaticText(self.panel, -1, "仪器列表：", (20,5), (100,20))
        NameList = get_instruments_list()
        #self.listBox = wx.ListBox(self.panel, -1, (20,20), (400,120), NameList, wx.LB_SINGLE )
        self.listBox = wx.Choice(self.panel, -1, (20,30), (400,120), NameList )
        self.Bind( wx.EVT_CHOICE, self.ChoiceChange, self.listBox )
        
        #self.listBox.SetSelection(0)
    def ChoiceChange(self, event):
        self.OpenInst()
         
    def OpenInst(self):  
        self.Conn = False
        if self.listBox.GetCurrentSelection() != -1:
            instname =  self.listBox.GetString(self.listBox.GetCurrentSelection())
            #print instname
            self.inst = instrument(instname)              
            self.readBox.Clear()    
            self.Conn = True           
    def TestButtClick(self, event): 
        self.OpenInst()
        if self.Conn:
            self.readBox.Clear()
            self.readBox.AppendText( self.inst.ask( "*idn?") ) 
            
    def OnMove(self, event):
        pos = event.GetPosition()
        self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show(True)
    app.MainLoop()